﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;

namespace Lingo
{
    public partial class Word : UserControl
    {
        private const int LENGTH = 5;
        public Letter[] Letters { get; set; }

        public Word(string word)
        {
            if (word.Length != LENGTH)
                throw new Exception("Word should be 5 characters!");

            this.Letters = new Letter[LENGTH];
            for (int i = 0; i < LENGTH; i++)
            {
                this.Letters[i] = new Letter(word[i]);
            }
            // First letter can be revealed.
            this.Letters[0].State = LetterState.Correct;

            DataContext = this;

            InitializeComponent();
        }

        public Word(Word word)
        {
            this.Letters = word.Letters;
            DataContext = word;

            InitializeComponent();
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < LENGTH; i++)
            {
                if (this.Letters[i].State == LetterState.Correct || this.Letters[i].State == LetterState.Present)
                    builder.Append(this.Letters[i].Character);
                else if (this.Letters[i].State == LetterState.Blank)
                    builder.Append('_');
            }
            return builder.ToString();
        }

        public string Solution
        {
            get
            {
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < LENGTH; i++)
                {
                    builder.Append(this.Letters[i].Character);
                }
                return builder.ToString();
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < LENGTH; i++)
            {
                TextBlock LetterTextBlock = new TextBlock();
                LetterTextBlock.Text = this.ToString()[i].ToString().ToUpper();
                // Some basic layout
                LetterTextBlock.FontSize = 48;
                LetterTextBlock.Margin = new Thickness(0, 0, 65, 0);
                WordStackPanel.Children.Add(LetterTextBlock);
                if (this.Letters[i].State == LetterState.Correct)
                    LetterTextBlock.Foreground = new SolidColorBrush(Colors.Green);
                else if (this.Letters[i].State == LetterState.Present)
                    LetterTextBlock.Foreground = new SolidColorBrush(Colors.Red);
            }
        }
    }

    public struct Letter
    {
        public LetterState State { get; set; }
        public char Character { get; set; }

        public Letter(char letter) : this()
        {
            this.Character = letter;
            this.State = LetterState.Blank;
        }
    }

    public enum LetterState
    {
        Blank,
        Correct,
        Present
    }
}
